#!/bin/bash
root_dir="/usr/local/edata"
if [ ! -d "$root_dir" ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir undetected"
    mkdir -p $root_dir
    if [ $? -eq 0 ];then
        echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating $root_dir success"
    else
        echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating $root_dir fail"
        exit 1
    fi
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir detected"
fi

jdk_ver=$(java -version 2>&1)
if [[ $jdk_ver == *"1.8.0_221"* ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO jdk 1.8.0_221 detected"
else
    if [[ $jdk_ver == *"not found"* ]];then
        echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING jdk is not found,installing..."
    else
        echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR other version of jdk detected (1.8.0_221 is required),uninstall it first"
        exit 1
    fi
fi


echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking kafka directory ..."
if [ ! -d "$root_dir/kafka" ];then
	echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir/kafka is not found...,unpressing kafka package of edata"
	tar -zxvf kafka_2.13-3.5.0.tgz
	mv kafka_2.13-3.5.0 $root_dir/kafka
    rm $root_dir/kafka/config/kraft/server.properties
    rm $root_dir/kafka/config/kraft/broker.properties
    rm $root_dir/kafka/config/kraft/controller.properties
    mkdir -p $root_dir/kafka/logs
    mkdir -p $root_dir/kafka/metadata-logs
    mkdir -p $root_dir/kafka/custom
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") $root_dir/kafka detected"
fi

echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking KAFKA_HOME ..."
if [[ -z "$KAFKA_HOME" ]];then
	echo "export KAFKA_HOME=$root_dir/kafka" >> ~/.bashrc
	echo 'export PATH=$KAFKA_HOME/bin:$PATH' >> ~/.bashrc
	echo "export KAFKA_HOME=$root_dir/kafka" >> /etc/profile
	echo 'export PATH=$KAFKA_HOME/bin:$PATH' >> /etc/profile
	source ~/.bashrc
	source /etc/profile
else
	echo "$(date +"%Y-%m-%d %H:%M:%S") INFO KAFKA_HOME is set"
fi

mode=$1
if [[ -z "$mode" ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR kafka mode is empty"
  exit 1
fi

node_id=$2
if [[ -z "$node_id" ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR node id is empty"
  exit 1
fi

echo $mode >> mode
echo $2 >> node_id
env_file="kafka_single.env"
if [ $mode == "cluster" ];then
	env_file="kafka_cluster.env"
fi

conf_dir=$root_dir/kafka/config
while IFS= read -r line
do
	if [[ -n "$line" ]];then
        IFS="_" read -r prefix suffix <<< "$line"
		IFS="=" read -r name value <<< "$suffix"
        echo "$(date +"%Y-%m-%d %H:%M:%S") setting $name=$value"
        if [ $prefix == "KF" ];then
            echo "$name=$value" >> $conf_dir/kraft/server.properties
        fi
	fi
done < "$env_file"
kafka-storage.sh random-uuid >> cluster_id
if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating clusterid success"
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating clusterid error"
fi

kafka-server-start.sh --version
if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO kafka setup success"
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR kafka setup error"
fi